package com.mew.chess.core.game.bjl.game.task;

import cn.hutool.core.util.RandomUtil;
import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.bjl.game.BjlGameManager;
import com.mew.chess.core.game.bjl.game.BjlRoomManager;
import com.mew.chess.core.game.bjl.game.common.BjlConstantValue;
import com.mew.chess.core.game.bjl.game.common.BjlLinkType;
import com.mew.chess.core.game.bjl.game.common.BjlPushCMDMapType;
import com.mew.chess.core.game.bjl.game.message.BetRecordMessage;
import com.mew.chess.core.game.bjl.game.message.PushBetMessage;
import com.mew.chess.core.game.bjl.game.message.UserMessage;
import com.mew.common.enm.UserType;
import com.mew.common.util.GameUtil;
import com.mew.common.util.NumberUtil;
import com.zebra.common.concurrent.ZebraScheduler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.stream.Collectors;

/**
 * 推送机器人压注
 * @author dylin
 * @create 2021/4/22
 */
@Component
@Slf4j
public class BjlPushBetScheduleTask {
    public void robotBet(int roomId, ScheduledFuture<?> stepFuture){ ;
        BjlRoomManager roomManager = BjlGameManager.getInstance().getController(roomId);
        if (roomManager.getLinkType() != BjlLinkType.StartBet.code() || roomManager.getRobotNumber() == 0){
            if (null != stepFuture){
                ZebraScheduler.cancelScheduled(stepFuture);
            }
            return;
        }

        int[] betWeight={
                NumberUtil.randomIntByStr(BjlConstantValue.ROBOT_BANKER_WIN_SEC,"-"),
                NumberUtil.randomIntByStr(BjlConstantValue.ROBOT_IDLE_WIN_SEC,"-"),
                NumberUtil.randomIntByStr(BjlConstantValue.ROBOT_EQUAL_WIN_SEC,"-"),
                NumberUtil.randomIntByStr(BjlConstantValue.ROBOT_BANKER_TWO_WIN_SEC,"-"),
                NumberUtil.randomIntByStr(BjlConstantValue.ROBOT_IDLE_TWO_WIN_SEC,"-"),
        };
        //随机压注数量 机器人人数的百分之5到10之间的人数
        int betRobotNumber=RandomUtil.randomInt(
                (int)Math.floor(roomManager.getRobotNumber()/40.0d),(int)Math.ceil(roomManager.getRobotNumber()/20.0d)+1);
        BetRecordMessage[] bets=new BetRecordMessage[betRobotNumber];
        //机器人列表
        List<UserMessage> robotList=roomManager.getPlayerList().values().stream().filter(u-> u.getUserType()==UserType.Robot.code()).collect(Collectors.toList());
        for (int i = 0; i < betRobotNumber; i++) {
            UserMessage robot =robotList.get(RandomUtil.randomInt(robotList.size()));
            int betType = GameUtil.weights(betWeight);
            int betNumber = roomManager.getBetArrays()[GameUtil.weights(BjlConstantValue.ROBOT_BET_PROB)];
            robot.userBet(betType,betNumber);

            bets[i]=BetRecordMessage.builder()
                    .uid(roomManager.isBigPlayer(robot.getUserId())?robot.getUserId():-1)
                    .betType(betType)
                    .betNumber(betNumber)
                    .build();
        }
        PushBetMessage pushBetMessage=roomManager.bets(bets);
        ChessPushMessageUtil.broadcastMessage(roomManager.getOnlineUser().values(),BjlPushCMDMapType.BetInfoMessage.code(),pushBetMessage);
    }

    public static void main(String[] args) {
        List<Long> list=new ArrayList<>();
        list.add(2000L);
        list.add(6000L);
        list.add(3000L);
        list.add(8000L);
        Collections.sort(list,(u1, u2)->Long.compare(u2,u1));
        for (Long aLong : list) {
            System.out.println(aLong);
        }
    }
}
